package com.tianqu.tms.www.assess.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinanceInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.assess.viewhelper.AssessCarInfoInputViewHelper;

/**
 * 
 * 车辆评估直接录入数据库部分
 * 
 * @author Administrator
 * 
 */
public class CarAssessInputResultController extends TmsController {

	private Logger logger = Logger
			.getLogger(CarAssessInputResultController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			AssessCarInfoInputViewHelper view = (AssessCarInfoInputViewHelper) uc
					.getAttribute(TmsConstants.VIEWHELP);
			TmsUserProfile profile = uc.getUserProfile();
			int marketType = view.getMarketType();
			if (marketType == 2) {
				TransferPersonalCarInfo person = new TransferPersonalCarInfo();
				person = setPersonEntityFromView(person, view);
				person.setUpdateUserId(profile.getUniqueId());
				person.setPrice(Integer.parseInt(StringUtil.removeDH(view.getAssessPirce())));// 评估价格
				person.updateCarBaseInfoAtAssess(person);
			}
			if (marketType == 1) {
				Car car = new Car();
				car = setCarEntityFromView(car, view);
				car.setUpdateUserId(profile.getUniqueId());
				car.setPrice(Integer.parseInt(StringUtil.removeDH(view.getAssessPirce())));// 评估价格
				car.updateCarInfoAtAssess(car);
			}

			TransferAcceptedInfo accept = new TransferAcceptedInfo();
			accept.setSerial(view.getSerial());
			accept.setVin(view.getVin());
			accept.setMarketType(view.getMarketType());
			accept.setPg_price(Integer.parseInt(StringUtil.removeDH(view
					.getAssessPirce())));
			accept.setUpdateUserId(profile.getUniqueId());
			accept.setStatus(4);
			TransferAcceptedInfo accept1 = accept.findPirntNumBySerial(view
					.getSerial());
			accept.setPg_printNum(accept1.getPg_printNum() + 1);
			accept.setPrice(Integer.parseInt(StringUtil.removeDH(view
					.getAssessPirce())));// 评估价格
			accept.setCarOwner(view.getCarOwner());

			accept.updateAcceptInfoAtAssess(accept);

			// 更新财务收费表
			TransferFinanceInfo finance = new TransferFinanceInfo();
			finance.setSerial(view.getSerial());
			finance.setAssessPrice((int) (Integer.parseInt(StringUtil
					.removeDH(view.getAssessPirce())) * 0.0192));
			finance.updateFinanceInfoAtAssess(finance);

			// 历史表添加信息
			TransferProcessHistory history = new TransferProcessHistory();
			history.setSerial(view.getSerial());
			history.setBusinessType(4);
			history.setInsertUserId(profile.getUniqueId());
			history.setReason("");
			history.insertHisInfoAtAssess(history);

			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	/**
	 * 将view中信息set到entity中
	 * 
	 * @throws TmsDaoException
	 */
	private TransferPersonalCarInfo setPersonEntityFromView(
			TransferPersonalCarInfo entity, AssessCarInfoInputViewHelper view)
			throws TmsDaoException {
		entity.setSerial(view.getBasicCarId());
		entity.setLicensePlate(view.getLicensePlate());
		entity.setVin(view.getVin());
		entity.setVendorCode(view.getVendorCode());
		entity.setModelCode(view.getModelCode());
		entity.setCarType(Integer.parseInt(view.getCarType()));
		if (view.getColor() != null && !"".equals(view.getColor())) {
			entity.setColor(Integer.parseInt(view.getColor()));
		} else {
			entity.setColor(0);
		}
		entity.setCarUse(Integer.parseInt(view.getCarUse()));
		entity.setRegDate(view.getRegDate());
		entity.setSendCardDate(view.getSendCardDate());
		entity.setCarOwner(view.getCarOwner());

		return entity;
	}

	/**
	 * 将view中信息set到entity中
	 * 
	 * @throws TmsDaoException
	 */
	private Car setCarEntityFromView(Car entity,
			AssessCarInfoInputViewHelper view) throws TmsDaoException {
		entity.setSerial(view.getBasicCarId());
		entity.setLicensePlate(view.getLicensePlate());
		entity.setVin(view.getVin());
		entity.setVendorCode(view.getVendorCode());
		entity.setModelCode(view.getModelCode());
		entity.setCarType(Integer.parseInt(view.getCarType()));
		if (view.getColor() != null && !"".equals(view.getColor())) {
			entity.setColor(Integer.parseInt(view.getColor()));
		} else {
			entity.setColor(0);
		}
		entity.setCarUse(Integer.parseInt(view.getCarUse()));
		entity.setRegDate(view.getRegDate());
		entity.setSendCardDate(view.getSendCardDate());
		entity.setCarOwner(view.getCarOwner());

		return entity;
	}

	public static void main(String[] args) {
		System.out.println(1000 * 0.192);
	}
}